package com.zjy.software.day17;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class OtherTypeDemo {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        Class<Person> personClass = Person.class;
        Constructor[] constructors = personClass.getConstructors();
        for (Constructor<?> constructor : constructors) {
            System.out.println("constructor.getName() = " + constructor.getName());
            System.out.println("constructor.getModifiers() = " + constructor.getModifiers());
            System.out.println("constructor.getParameterCount() = " + constructor.getParameterCount());
        }

        Constructor<Person> constructor = personClass.getConstructor(String.class);
        System.out.println("constructor = " + constructor);
        Person person = constructor.newInstance("张三");
        System.out.println("person = " + person);

        Class<Teacher> teacherClass = Teacher.class;
        //默认获取当前类的父类,如果当前类没有父类,则返回Object
        Class<? super Teacher> superclass = teacherClass.getSuperclass();
        System.out.println("superclass = " + superclass);

        //得到当前类的接口
        Class<?>[] interfaces = teacherClass.getInterfaces();
        for (Class<?> anInterface : interfaces) {
            System.out.println("anInterface = " + anInterface);
        }

        //获取包名
        Package aPackage = teacherClass.getPackage();
        System.out.println("aPackage.getName() = " + aPackage.getName());

        System.out.println("aPackage = " + aPackage);

        Field address = teacherClass.getDeclaredField("address");
        System.out.println("address = " + address);
        //可以通过反射的语法,把一个私有属性设置为可访问,然后通过反射就可以直接访问
        address.setAccessible(true);
        Teacher obj = new Teacher();
        address.set(obj,"深圳");
        System.out.println("obj = " + obj);

    }
}
